home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Sample.bin / HtmlPanel.java < prev    next >
Text File  |  1998-06-30  |  4KB  |  139 lines

  1. /*
  2.  * @(#)HtmlPanel.java    1.7 98/02/02
  3.  *
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  *
  19.  */
  20.  
  21. import com.sun.java.swing.*;
  22. import com.sun.java.swing.event.*;
  23. import com.sun.java.swing.text.*;
  24. import com.sun.java.accessibility.*;
  25.  
  26. import java.awt.*;
  27. import java.net.URL;
  28. import java.net.MalformedURLException;
  29. import java.io.IOException;
  30.  
  31. /*
  32.  * @version 1.7 02/02/98
  33.  * @author Jeff Dinkins
  34.  * @author Tim Prinzing
  35.  * @author Peter Korn (accessibility support)
  36.  */
  37. public class HtmlPanel extends JPanel implements HyperlinkListener {
  38.     SwingSet swing;
  39.     JEditorPane html;
  40.  
  41.     public HtmlPanel(SwingSet swing) {
  42.     this.swing = swing;
  43.         // setBackground(Color.white);
  44.     setBorder(swing.emptyBorder10);
  45.         setLayout(new BorderLayout());
  46.     getAccessibleContext().setAccessibleName("HTML panel");
  47.     getAccessibleContext().setAccessibleDescription("A panel for viewing HTML documents, and following their links");
  48.     
  49.     try {
  50.         URL url = new URL(swing.javaDocPath + "/packages.html");
  51.         html = new JEditorPane(url);
  52.         html.setEditable(false);
  53.         html.addHyperlinkListener(this);
  54.         JScrollPane scroller = new JScrollPane();
  55.         scroller.setBorder(swing.loweredBorder);
  56.         JViewport vp = scroller.getViewport();
  57.         vp.add(html);
  58.         vp.setBackingStoreEnabled(true);
  59.         add(scroller, BorderLayout.CENTER);
  60.     } catch (MalformedURLException e) {
  61.         System.out.println("Malformed URL: " + e);
  62.     } catch (IOException e) {
  63.         System.out.println("IOException: " + e);
  64.     }
  65.     
  66.     }
  67.  
  68.     /**
  69.      * Notification of a change relative to a 
  70.      * hyperlink.
  71.      */
  72.     public void hyperlinkUpdate(HyperlinkEvent e) {
  73.     if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
  74.         linkActivated(e.getURL());
  75.     }
  76.     }
  77.  
  78.     /**
  79.      * Follows the reference in an
  80.      * link.  The given url is the requested reference.
  81.      * By default this calls <a href="#setPage">setPage</a>,
  82.      * and if an exception is thrown the original previous
  83.      * document is restored and a beep sounded.  If an 
  84.      * attempt was made to follow a link, but it represented
  85.      * a malformed url, this method will be called with a
  86.      * null argument.
  87.      *
  88.      * @param u the URL to follow
  89.      */
  90.     protected void linkActivated(URL u) {
  91.     Cursor c = html.getCursor();
  92.     Cursor waitCursor = Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR);
  93.     html.setCursor(waitCursor);
  94.     SwingUtilities.invokeLater(new PageLoader(u, c));
  95.     }
  96.  
  97.     /**
  98.      * temporary class that loads synchronously (although
  99.      * later than the request so that a cursor change
  100.      * can be done).
  101.      */
  102.     class PageLoader implements Runnable {
  103.     
  104.     PageLoader(URL u, Cursor c) {
  105.         url = u;
  106.         cursor = c;
  107.     }
  108.  
  109.         public void run() {
  110.         if (url == null) {
  111.         // restore the original cursor
  112.         html.setCursor(cursor);
  113.  
  114.         // PENDING(prinz) remove this hack when 
  115.         // automatic validation is activated.
  116.         Container parent = html.getParent();
  117.         parent.repaint();
  118.         } else {
  119.         Document doc = html.getDocument();
  120.         try {
  121.             html.setPage(url);
  122.         } catch (IOException ioe) {
  123.             html.setDocument(doc);
  124.             getToolkit().beep();
  125.         } finally {
  126.             // schedule the cursor to revert after
  127.             // the paint has happended.
  128.             url = null;
  129.             SwingUtilities.invokeLater(this);
  130.         }
  131.         }
  132.     }
  133.  
  134.     URL url;
  135.     Cursor cursor;
  136.     }
  137.  
  138. }
  139.